<template>
  <div class="app-container">
    <el-dialog :title="'选择车辆'" append-to-body
               :visible.sync="isShowCar"
               :close-on-click-modal="false"
               width="80%"
               class="dialog_collect"
               @open="handleOpenDialog"
               @close="handleCloseDialog">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号码" prop="licensePlate">
        <el-input
          v-model="queryParams.licensePlate"
          placeholder="请输入车牌号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车型" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入车型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="carList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键id" align="center" prop="id" v-if="false"/>
      <el-table-column label="使用状态" align="center" prop="lamp" width="180">
        <template slot-scope="scope">
          <el-tooltip placement="top" content="正常" effect="dark" v-if="scope.row.lamp === '1'">
            <img src="/image/car/green-lamp.png"/>
          </el-tooltip>
          <el-tooltip placement="top" content="即将到期" effect="dark" v-if="scope.row.lamp === '2'">
            <img src="/image/car/red-lamp.png"/>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="在线状态" align="center" prop="isOnline">
        <template slot-scope="scope">
          <el-tooltip placement="top" content="在线" effect="dark" v-if="scope.row.isOnline === false">
            <img src="/image/car/green-lamp.png"/>
          </el-tooltip>
          <el-tooltip placement="top" content="离线" effect="dark" v-if="scope.row.isOnline === true">
            <img src="/image/car/red-lamp.png"/>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="终端名称" align="center" prop="equipmentName"/>
      <el-table-column label="终端编号" align="center" prop="equipmentNumber"/>
      <el-table-column label="车牌号码" align="center" prop="licensePlate"/>
      <el-table-column label="车型" align="center" prop="model"/>
      <el-table-column label="发动机号码" align="center" prop="engineNumber"/>
      <el-table-column label="底盘号码" align="center" prop="chassisNumber"/>
      <el-table-column label="责任人" align="center" prop="cadre"/>
      <el-table-column label="联系方式" align="center" prop="contact"/>
      <el-table-column label="车类型" align="center" prop="type"/>
      <el-table-column label="使用年限" align="center" prop="serviceLife"/>
      <el-table-column label="使用截止日期" align="center" prop="useDeadline" width="180">
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
      <section slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="handleCloseDialog">取 消</el-button>
      </section>
    </el-dialog>
  </div>
</template>

<script>
  import { listCar } from '@/api/basicInformation/car'
  import { validatenull } from '../../../../utils/validate'
  export default {
    name: 'Car',
    props: {
      isShowCar: {
        type: Boolean,
        default: false
      },
      collected3: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 车辆信息管理表格数据
        carList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          licensePlate: undefined,
          model: undefined
        },
      }
    },
    methods: {

      /** 查询车辆信息管理列表 */
      getList() {
        this.loading = true
        listCar(this.queryParams).then(response => {
          this.carList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        this.multipleSelection = selection
      },
      handleOpenDialog(){
        let carIds = this.collected3
        carIds = carIds.substring(0, carIds.lastIndexOf(','))
        this.queryParams.carIds = carIds
        this.getList()
      },
      handleCloseDialog() {
        this.$emit('closeDialog')
      },
      submitForm(){
        if (validatenull(this.multipleSelection)) {
          this.$message.warning('请选择车辆!')
          return false
        }
        let list = ''
        const table = []
        this.multipleSelection.forEach((item, index) => {
          let ta = {}
          ta.id = item.id
          ta.name = item.licensePlate
          table.push(ta)
          list += item.id + ','
        })
        this.handleSave(list, table)
      },
      handleSave(list, table) {
        const data = []
        data.push(list)
        data.push(table)
        this.handleCloseDialog()
        setTimeout(() => {
          this.$emit('ListSel', data)
        }, 10)
      }
    }
  }
</script>
<style lang="scss" scoped>
  ::v-deep .el-dialog__footer {
    text-align: center;
    position: absolute;
    left: 0;
    right: 0;
    bottom: -65px;
    padding: 15px;
    background: #f1f1f1;
  }
</style>
